<template>
  <div>
    <div>
      <el-row id="artList" type="flex" justify="space-around">
        <el-col>
          <el-row class="art-item">
            <ul class="list">
              <li v-for="item in postsList" v-bind:key="item" class="list-item">
                <el-card shadow="hover">
                  <router-link
                    :to="{ name: '详情', params: { postsId: item.id } }"
                  >
                    <h3>
                      <span class="art-title">{{ item.title }}</span>
                      <el-tag size="mini"></el-tag>
                    </h3>
                  </router-link>
                  <el-row class="art-body">
                    <div class="side-abstract">
                      <div class="art-abstract">{{ item.content }}</div>
                    </div>
                  </el-row>
                  <el-row class="art-info">
                    <div class="message" id="box">
                      <ul class="msg-ul">
                        <li class="msg">
                          <div>
                            <img class="picture" :src="item.user.avatar" />
                            <i style="line-height:30px;color:#aaa;">
                              {{ item.user.username }}
                            </i>
                          </div>
                        </li>
                        <li class="msg">
                          <span class="el-icon-time">{{
                            item.createTime
                          }}</span>
                        </li>
                        <li class="msg">
                          <i class="el-icon-view">{{ item.views }}</i>
                        </li>
                        <li class="msg">
                          <i class="el-icon-chat-dot-square">
                            {{ item.comment }}
                          </i>
                        </li>
                      </ul>
                      <router-link
                        :to="{ name: '编辑', params: { postsId: item.id } }"
                      >
                        <el-button style="float:right;" @click="toEdit()"
                          >编辑</el-button
                        >
                      </router-link>

                      <el-popconfirm
                        title="确定删除吗？"
                        @onConfirm="Deleted(item.id)"
                      >
                        <el-button slot="reference" style="float:right;"
                          >删除</el-button
                        >
                      </el-popconfirm>
                    </div>
                  </el-row>
                </el-card>
              </li>
            </ul>
          </el-row>
        </el-col>
      </el-row>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :current-page="currentPage"
        :page-size="pageSize"
        @current-change="page"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    categoryId: {
      type: Number,
      default() {
        return 0;
      },
    },
    title: {
      type: String,
      default() {
        return 0;
      },
    },
  },
  data() {
    return {
      id: this.categoryId,
      postsList: [],
      currentPage: 1,
      total: 0,
      pageSize: 10,
      poststitle: this.title,
    };
  },
  computed: {},
  created: function() {
    this.page(1);
  },

  methods: {
    //删除帖子
    Deleted(id) {
      const _this = this;
      this.$axios
        .post("/forum/posts/deletePostById?postsId=" + id)
        .then((res) => {
          if (res.data.code === 200) {
            this.$message({
              message: res.data.msg,
              type: "success",
            });
            this.page(1);
          } else {
            this.$message({
              message: res.data.msg,
              type: "error",
            });
          }
        });
    },

    // 帖子初始获取
    page(curr) {
      const _this = this;
      const userId = this.$store.state.userInfo.id;
      this.$axios
        .post("/forum/posts/getPostsById?curr=" + curr + "&id=" + userId)
        .then((res) => {
          _this.postsList = res.data.data.list;
          _this.total = res.data.data.total;
          _this.currentPage = res.data.data.current;
          _this.pageSize = res.data.data.size;
        });

      //获取total帖子总数
      // this.$axios.post("/forum/posts/getTotalCount").then((res)=>{
      //   console.log("帖子总数",res.data.data);
      //   _this.total=res.data.data;
      // })
    },
  },
};
</script>

<style scoped>
#box * {
  vertical-align: middle;
}
#side .item {
  margin-bottom: 30px;
}
.el-tag {
  margin-left: 5px;
  vertical-align: middle;
}
.picture {
  width: 30px;
  height: 30px;
  margin-left: 0px;
  margin-right: 5px;
}
.message {
  text-align: left;
  line-height: 30px;
  color: #aaa;
}
.msg-ul {
  margin: 0px;
}

.msg {
  float: left;
  list-style: none;
  line-height: 30px;
  margin-right: 30px;
}
.art-info {
  text-align: left;
}
.art-item {
  position: relative;
}
h3 {
  margin: 0px;
}
.art-item .star {
  width: 60px;
  height: 60px;
  position: absolute;
  top: 0;
  right: 0;
}
.infinite-list-wrapper {
  overflow-x: hidden;
  overflow-y: scroll;
}
.art-title {
  cursor: pointer;
}
.art-title:hover {
  padding-left: 10px;
  color: #ff4040;
}
.art-body {
  display: flex;
  padding: 10px 0;
}
img.art-banner {
  width: 100%;
  height: 100%;
  transition: all 0.6s;
}
img.art-banner:hover {
  transform: scale(1.4);
}
.side-abstract {
  flex: 1;
  display: flex;
  flex-direction: column;
}
.art-abstract {
  flex: 1;
  color: #aaa;
  width: 700px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
h5 {
  font-size: 18px;
}
.pagination {
  background-color: #f9f9f9;
}
.infinite-list-wrapper {
  max-height: 900px;
}
.list-item {
  list-style: none;
  text-align: left;
  margin: 5px 0 8px 0;
}

.list-item .el-card:hover {
  box-shadow: 1px 2px 8px #74b9ff;
}
.list-item .el-card:hover div.art-abstract {
  color: #34495e;
}
li.msg:hover {
  color: black;
}
</style>
